package com.web.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DataSource {
    //1、提供数据库链接信息
    //             jdbc:mysql://ip地址:端口号/数据库名?访问参数
    static String url = "jdbc:mysql://localhost:3306/mysqltest?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false";
    static String name = "root";
    static String password = "07037079Aa";
    static {
        //2、加载MySQL的驱动类
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //3、获取数据库链接的对象
    public static Connection getConnection() throws SQLException {
       return DriverManager.getConnection(url, name, password);
    }
    //4、释放资源
    public static void close(Connection connection) throws SQLException {
        if(connection!=null)
            connection.close();
    }
}
